package charts;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import vo.Point;
import Utils.ChartUtils;

public class LineChart {

	static private XYSeriesCollection createDataset(ArrayList<Point> points) {
		final XYSeries first = new XYSeries("x axis", true, false);
		for (int i = 0; i < points.size(); i++) {
			Point point = points.get(i);
			first.add(point.getX(), point.getY());
		}
		final XYSeriesCollection dataset = new XYSeriesCollection();
		dataset.addSeries(first);
		return dataset;
	}

	static private XYSeriesCollection createDataset(ArrayList<Point> points1, ArrayList<Point> points2) {
		final XYSeries first = new XYSeries("first line", true, false);
		final XYSeries second = new XYSeries("second line", true, false);
		for (int i = 0; i < points1.size(); i++) {
			Point point = points1.get(i);
			first.add(point.getX(), point.getY());

		}
		for (int i = 0; i < points2.size(); i++) {
			Point point = points2.get(i);
			second.add(point.getX(), point.getY());
		}
		final XYSeriesCollection dataset = new XYSeriesCollection();
		dataset.addSeries(first);
		dataset.addSeries(second);
		return dataset;
	}

	static public String createChart(ArrayList<Point> points, String path, String title, int width, int height) {
		final XYSeriesCollection dataset = createDataset(points);
		NumberAxis x = new NumberAxis("x description");
		NumberAxis y = new NumberAxis("y description");

		JFreeChart chart = new JFreeChart(path, null, new XYPlot(dataset, x, y, new StandardXYItemRenderer()), true);
		chart.setTitle(new TextTitle(title));
		try {
			ChartUtilities.saveChartAsJPEG(new File(path), 1, chart, width, height);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	static public String createChart(ArrayList<Point> points1, ArrayList<Point> points2, String path, String title, int width, int height) {
		final XYSeriesCollection dataset = createDataset(points1, points2);
		NumberAxis x = new NumberAxis("x description");
		NumberAxis y = new NumberAxis("y description");

		JFreeChart jfreechart = new JFreeChart(path, null, new XYPlot(dataset, x, y, new StandardXYItemRenderer()), true);
		jfreechart.setTitle(new TextTitle(title));
		ChartUtils.saveChart(jfreechart, path);
		System.out.println("draw chart finish");

		return null;
	}

	public static void main(String args[]) {

	}
}